package server.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.demo.entity.Equipment;
import server.demo.entity.RentalOrder;
import server.demo.repository.RentalOrderRepository;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Service
public class RentalOrderService {
    
    @Autowired
    private RentalOrderRepository rentalOrderRepository;
    
    @Autowired
    private EquipmentService equipmentService;
    
    public List<RentalOrder> getAllOrders() {
        return rentalOrderRepository.findAll();
    }
    
    public Optional<RentalOrder> getOrderById(Long id) {
        return rentalOrderRepository.findById(id);
    }
    
    public Optional<RentalOrder> getOrderByOrderNo(String orderNo) {
        return rentalOrderRepository.findByOrderNo(orderNo);
    }
    
    public List<RentalOrder> getOrdersByUserId(Long userId) {
        return rentalOrderRepository.findByUserIdOrderByCreatedAtDesc(userId);
    }
    
    public List<RentalOrder> getOrdersByStatus(RentalOrder.OrderStatus status) {
        return rentalOrderRepository.findByStatus(status);
    }
    
    public Long getOrderCountByStatus(RentalOrder.OrderStatus status) {
        return rentalOrderRepository.countByStatus(status);
    }
    
    public RentalOrder createOrder(RentalOrder order) {
        Optional<Equipment> equipmentOpt = equipmentService.getEquipmentById(order.getEquipmentId());
        if (equipmentOpt.isEmpty()) {
            throw new RuntimeException("设备不存在");
        }
        
        Equipment equipment = equipmentOpt.get();
        if (equipment.getStatus() != Equipment.EquipmentStatus.AVAILABLE) {
            throw new RuntimeException("设备不可用");
        }
        
        order.setOrderNo(generateOrderNo());
        RentalOrder savedOrder = rentalOrderRepository.save(order);
        
        equipmentService.updateEquipmentStatus(order.getEquipmentId(), Equipment.EquipmentStatus.RENTED);
        
        return savedOrder;
    }
    
    public RentalOrder approveOrder(Long orderId) {
        Optional<RentalOrder> orderOpt = rentalOrderRepository.findById(orderId);
        if (orderOpt.isEmpty()) {
            throw new RuntimeException("订单不存在");
        }
        
        RentalOrder order = orderOpt.get();
        if (order.getStatus() != RentalOrder.OrderStatus.PENDING) {
            throw new RuntimeException("只能审批待处理状态的订单");
        }
        
        order.setStatus(RentalOrder.OrderStatus.ACTIVE);
        order.setRentalStartDate(LocalDate.now());
        
        return rentalOrderRepository.save(order);
    }
    
    public RentalOrder returnEquipment(Long orderId) {
        Optional<RentalOrder> orderOpt = rentalOrderRepository.findById(orderId);
        if (orderOpt.isEmpty()) {
            throw new RuntimeException("订单不存在");
        }
        
        RentalOrder order = orderOpt.get();
        if (order.getStatus() != RentalOrder.OrderStatus.ACTIVE) {
            throw new RuntimeException("订单状态不正确");
        }
        
        order.setActualReturnDate(LocalDate.now());
        order.setStatus(RentalOrder.OrderStatus.COMPLETED);
        
        equipmentService.updateEquipmentStatus(order.getEquipmentId(), Equipment.EquipmentStatus.AVAILABLE);
        
        return rentalOrderRepository.save(order);
    }
    
    private String generateOrderNo() {
        return "ORD" + System.currentTimeMillis();
    }
}